package components.editor
{
	import MLP.mlp_layer3d;
	
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import classes.c_util;
	
	import components.c_component;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	
	public class c_multiLayerElement extends c_baseElement {
		private var _layer3D			: mlp_layer3d;
		private var _mPosition			: Point;
		private var _waveIndex1			: Number;
		private var _waveIndex2			: Number;
		private var _curPos				: Point;
		
		private static const _density	: Number = 0.07;
		
		public function c_multiLayerElement(parent:uif_component, a_bitmaps: Array=null, a_depths: Object=0, ax:int=0, ay:int=0) {
			super(parent, ax, ay);
			_waveIndex1 = 0;
			_waveIndex2 = 0;
			_mPosition = mousePos;
			_layer3D = new mlp_layer3d();
			addChild(_layer3D);
			if (a_bitmaps) addLayers(a_bitmaps, a_depths);
			_curPos = parentComponent.mousePos;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(Event.ENTER_FRAME, doFrame, stage);
			addEvent(MouseEvent.MOUSE_MOVE, doMouseMove, stage);
		}
		
		override protected function nameClass(): String {
			return locale.MULTILAYER;
		}
		
		protected function doMouseMove(e: MouseEvent): void {
			if (parent) {
				if (parentComponent.parentComponent.contains(e.target as DisplayObject))
					_curPos = mousePos;
				else _curPos = center;
			}
		}
		
		protected function doFrame(e: Event): void {
			updateMatrix();
			_waveIndex1 += 0.075;
			_waveIndex2 += 0.05;
		}
		
		protected function updateMatrix(): void {
			var matrix: Matrix3D = new Matrix3D();
			var c: Point = center;
			matrix.appendTranslation(-c.x, -c.y, 0);
			
/*			
			var cc: Point = editor.center.subtract(position.add(c));
			matrix.append(c_util.lookAt(new Vector3D(-c.x, -c.y, z), 
				new Vector3D(cc.x - c.x, cc.y - c.y, 0), Vector3D.Z_AXIS));*/
			
			updateFromCursor(matrix);
			updateFromWave(matrix);
			
			matrix.appendTranslation(c.x, c.y, 0);				
			_layer3D.transform.matrix3D = matrix;
		}
		
		protected function updateFromWave(matrix: Matrix3D): void {
			var _wave: Number = (Math.sin(_waveIndex1) + Math.sin(_waveIndex2));
			matrix.appendRotation(_wave, Vector3D.Y_AXIS);
		}
		
		protected function updateFromCursor(matrix: Matrix3D): void {
			_mPosition = new Point(_mPosition.x + (_curPos.x - _mPosition.x) * _density, 
									_mPosition.y + (_curPos.y - _mPosition.y) * _density);
			
			var c: Point = center;
			var k: Number = -0.02;
			matrix.appendRotation((_mPosition.x - c.x) * k, Vector3D.Y_AXIS);
			matrix.appendRotation((_mPosition.y - c.y) * k, Vector3D.X_AXIS);
		}
		
		public function addLayers(a_layers: Array, a_depths: Object=0): void {
			_layer3D.addLayers(a_layers, a_depths);			
			resize(_layer3D.layersRect.size.x, _layer3D.layersRect.size.y);
		}
		
		public function get layer3D(): mlp_layer3d {
			return _layer3D;
		}
		
		public function set layer3D(a_layer: mlp_layer3d): void {
			if (_layer3D) _layer3D.dispose();
			_layer3D = a_layer;
			addChild(_layer3D);
		}
	}
}